import modelExtend from 'dva-model-extend'
import { pathMatchRegexp } from 'utils'
import api from 'api'
import { pageModel } from 'utils/model'
import { message } from 'antd'

const { <#list fields as field><#if (field.type=='TreeSelect')>get${field.name?cap_first}Tree,</#if></#list>query${api?cap_first},create${api?cap_first},update${api?cap_first},remove${api?cap_first} }=api

export default modelExtend(pageModel, {
	namespace: '${namespace}',

	state: {
		modalVisible:false,
		currentItem: {},
		currentDetail:{},
		itemDetails:[],
		filterItem:{},<#list fields as field><#if (field.type=='TreeSelect')>
		${field.name}Tree:[],</#if></#list>
	},

	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(location => {
				const {pathname, query}=location
				if (pathMatchRegexp('${url}', pathname)) {
					const payload = {...query, page: 1, pageSize: 10 }
					dispatch({
						type: 'query',
						payload,
					})
					<#list fields as field><#if (field.type=='TreeSelect')>
					dispatch({type: 'get${field.name?cap_first}Tree'})
					</#if></#list>
				}
			})
		},
	},

	effects: {
		*query({ payload = {} }, { call, put }) {
			const data = yield call(query${api?cap_first}, payload)
			const {error,result}=data
			if (!error) {
				yield put({
					type: 'querySuccess',
					payload: {
						list: result.rows,
						pagination: {
							current: Number(payload.page) || 1,
							pageSize: Number(payload.pageSize) || 10,
							total: result.total,
						},
					},
				})
			}
		},
		<#list fields as field><#if (field.type=='TreeSelect')>
		*get${field.name?cap_first}Tree({ payload = {} }, { call, put }) {
			const {success,result} = yield call(get${field.name?cap_first}Tree, payload)
			if (success) {
				yield put({
					type: 'updateState',
					payload: {
						${field.name}Tree:result
					},
				})
			}
		},</#if></#list>
		
		*remove({ payload }, { call, put, select }) {
			const {success} = yield call(remove${api?cap_first}, payload)
			if (success) {
				message.success('操作成功')
			} else {
				message.error('操作失败')
			}
		},

		*create({ payload }, { call, put }) {
			const {success} = yield call(create${api?cap_first}, payload)
			if (success) {
				message.success('操作成功')
				yield put({ type: 'hideModal' })
			} else {
				message.error('操作失败')
			}
		},

		*update({ payload }, { select, call, put }) {
			const id = yield select(({ ${namespace} }) => ${namespace}.currentItem.id)
			const obj = { ...payload, id }
			const {success} = yield call(update${api?cap_first}, obj)
			if (success) {
				message.success('操作成功')
				yield put({ type: 'hideModal' })
			} else {
				message.error('操作失败')
			}
		},
		
		*addDetail({ payload }, { select, call, put }) {
			const details = yield select(({ ${namespace} }) => ${namespace}.itemDetails)
			details.push({id:-(new Date().getTime())})
			yield put({
				type: 'updateState',
				payload: {
					itemDetails:itemDetails
				}
			})
		},
	},

	reducers: {
		showModal(state, { payload }) {
			return { ...state, ...payload, modalVisible: true }
		},

		hideModal(state) {
			return { ...state, modalVisible: false }
		},
	},

})
